
import pandas as pd
from common.config import opcUaConfig as Config
from opcUaSrc.plc.tools import *
import time
from common.config import commonConfig as Config
from toolsSrc.dbtools.dbtools import DBTools
from sqlalchemy import text

def saveDBData1(commonNodes):
    data1 = getData1(commonNodes)
    url = Config.url
    # url = 'mysql+pymysql://root:123456@localhost:3306/eladmin?charset=utf8'
    session = DBTools(url).connectDB()

    sqlStr = "UPDATE data1 SET "
    for key in data1.keys():
        sqlStr += "`"+key+"`=" + str(data1[key][0]) + " ,"
    sqlStr = sqlStr[:-1]  # 取到最后一个逗号
    # print("sqlStr: ", sqlStr)

    result = session.execute(
        text(sqlStr)
    )

    # result = session.execute(
    #     text("UPDATE data1 SET a1=:a1 ,a2=:a2"), [{"a1": data1.iloc[0,0], "a2": data1.iloc[0,1]}])
    session.commit()
    session.close()

# def getDBData1():
#     url = Config.url
#     # url = 'mysql+pymysql://root:123456@localhost:3306/eladmin?charset=utf8'
#     session = DBTools(url).connectDB()
#     result = session.execute(
#         text("select * from data1"))
#     print(result.all())
#     session.commit()
#     session.close()
#     return result

if __name__ == '__main__':
    url = Config.url
    session1 = DBTools(url).connectDB()
    session2 = DBTools(url).connectDB()
    print(session1==session2)
